package com.beiing.tianshuai.tianshuai.fragment;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;

import com.mic.etoast2.Toast;

import butterknife.Unbinder;

/**
 * Created by Xavier on 2017/7/20 0020.
 */

public abstract class BaseLazyLoadFragment extends Fragment {

    public View mContentView;
    public Context mContext;
    public boolean isVisible = false;           // 判断当前用户是否可见
    public Unbinder mUnbinder;
    public boolean isPrepared;
    public boolean isLoadedOnce;

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isVisible = isVisibleToUser;
        if (isVisibleToUser) {
            onVisible();
        } else {
            onInvisible();
        }
    }

    /**
     * 可见
     */
    protected void onVisible() {
        lazyLoad();
    }

    /**
     * 不可见
     */
    protected void onInvisible() {
    }

    /**
     * 延迟加载 定义成抽象的这样子类必须重写此方法
     */
    protected abstract void lazyLoad();

    public void showShortText(String text) {
        if (getActivity() != null) {
            Toast.makeText(getActivity(), text, 0).show();
        }
    }

    public void showLongText(String text) {
        Toast.makeText(getActivity(), text, 1).show();
    }
}
